package com.design.state;

/**
 * @author leali
 * @title: Switch
 * @projectName design
 * @description: 共享狀態開個
 * @date 2021/12/31 22:03
 */
public class Switch {
    private static SwitchState state, onState, offState; //定义三个静态的状态对象
    private final String name;

    public Switch(String name) {
        this.name = name;
        onState = new OnState();
        offState = new OffState();
        state = onState;
    }

    public void setState(SwitchState state) {
        Switch.state = state;
    }

    public static SwitchState getState(String type) {
        if (type.equalsIgnoreCase("on")) {
            return onState;
        } else {
            return offState;
        }
    }

    //打开开关
    public void on() {
        System.out.print(name);
        state.on(this);
    }

    //关闭开关
    public void off() {
        System.out.print(name);
        state.off(this);
    }
}
